/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package businesscard.DAO;

import bc.entity.Nganh;
import bc.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Welcome
 */
public class NganhDAO {
    private Session session;

    public NganhDAO() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    public Nganh[] getAllNganh()  {     
        try {
            session.getTransaction().begin();
            String sql = "from Nganh";
            Query query = session.createQuery(sql);
            List result=query.list();
           Nganh[] nganhList = new Nganh[result.size()];
            result.toArray(nganhList);
            session.flush();
            session.getTransaction().commit();
            return nganhList;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;

    }
    public Nganh[] selectNganh(int id_nhomNganh){
        try{
            session.getTransaction().begin();
            String sql = "from Nganh where id_nhomnganh = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0,id_nhomNganh );
            List result=query.list();
           Nganh[] nganhList = new Nganh[result.size()];
            result.toArray(nganhList);
            session.flush();
            session.getTransaction().commit();
            return nganhList;
        }catch(Exception e){
             if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
      //cuanga
    public Nganh getNganhbyID(int id) {
        try {
            session.getTransaction().begin();
            String sql = "from Nganh where id_nganh= ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            Nganh result = (Nganh) query.uniqueResult();

            session.flush();
            session.getTransaction().commit();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    // cua Nga
    public List<Nganh> getNganhlikeName(String name) {
        try {
            session.getTransaction().begin();
            String sql = "from Nganh where nganh like ?";
            Query query = session.createQuery(sql);
            query.setString(0, "%" + name + "%");
            List rs = query.list();
            // Congty[] arrct = new Congty[rs.size()];
            // rs.toArray(arrct);
            session.flush();
            return rs;

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();

        }
        return null;
    }
    public static void main(String[] args) {
        NganhDAO A=new NganhDAO();
        Nganh[] B=A.selectNganh(1);
        for(int i=0;i<B.length;i++){
            System.out.println("a ... "+B[i].getNganh());
        }
    }
}
